﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Mvc;
using Vit.Common;
using Vit.Web.Models;
using Vit.Web.Mvc.Results;
using Vit.Web.Providers;
using Vit.Web.Helpers;

namespace Vit.Web.Mvc.Controllers
{
    public class ControllerBase : System.Web.Mvc.Controller
    {
        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            if (requestContext.HttpContext.Session["ApplicationMessage"] != null)
            {
                ViewBag.Message = requestContext.HttpContext.Session["ApplicationMessage"];
                requestContext.HttpContext.Session.Remove("ApplicationMessage");
            }
        }

        protected override RedirectResult Redirect(string url)
        {
            EnsureMessages();
            return base.Redirect(url);
        }

        protected override RedirectResult RedirectPermanent(string url)
        {
            EnsureMessages();
            return base.RedirectPermanent(url);
        }

        protected override RedirectToRouteResult RedirectToAction(string actionName, string controllerName, RouteValueDictionary routeValues)
        {
            EnsureMessages();
            return base.RedirectToAction(actionName, controllerName, routeValues);
        }

        protected override RedirectToRouteResult RedirectToActionPermanent(string actionName, string controllerName, RouteValueDictionary routeValues)
        {
            EnsureMessages();
            return base.RedirectToActionPermanent(actionName, controllerName, routeValues);
        }

        protected override RedirectToRouteResult RedirectToRoute(string routeName, RouteValueDictionary routeValues)
        {
            EnsureMessages();
            return base.RedirectToRoute(routeName, routeValues);
        }

        protected override RedirectToRouteResult RedirectToRoutePermanent(string routeName, RouteValueDictionary routeValues)
        {
            EnsureMessages();
            return base.RedirectToRoutePermanent(routeName, routeValues);
        }

        protected RefreshResult Refresh()
        {
            return new RefreshResult();
        }

        protected GoBackResult GoBack()
        {
            return new GoBackResult();
        }

        protected void EnsureMessages()
        {
            object message = null;
            if (ViewBag.Message != null)
                message = ViewBag.Message;
            if (TempData["Message"] != null)
                message = TempData["Message"];

            if (message != null)
                this.Session["ApplicationMessage"] = message.ToString().Replace("'", "\"").Replace(Environment.NewLine, "<br/>");
        }

        protected PageMode PageMode
        {
            get
            {
                if (this.RouteData.Values.ContainsKey("pageMode"))
                    return (PageMode)this.RouteData.Values["pageMode"];
                return Models.PageMode.Visit;
            }
        }
    }
}
